<?php
	// This file is the place to store all basic functions

	function mysql_prep( $value )
	{
		$magic_quotes_active = get_magic_quotes_gpc();
		$new_enough_php = function_exists( "mysql_real_escape_string" ); // i.e. PHP >= v4.3.0
		if( $new_enough_php )
		{ // PHP v4.3.0 or higher
			// undo any magic quote effects so mysql_real_escape_string can do the work
			if( $magic_quotes_active )
				$value = stripslashes( $value );
			$value = mysql_real_escape_string( $value );
		}
		else
		{ 	// before PHP v4.3.0
			// if magic quotes aren't already on then add slashes manually
			if( !$magic_quotes_active )
				$value = addslashes( $value );
			// if magic quotes are active, then the slashes already exist
		}
		return $value;
	}
	
	function redirect_to( $location = NULL )
	{
		if ($location != NULL)
		{
			header("Location: {$location}");
			exit;
		}
	}
	
	function confirm_query($result_set)
	{
		if (!$result_set) {
			error_log("Database query failed: " . mysql_error());
		}
	}
	
	function get_all_subjects()
	{
		global $connection;
		$query = "SELECT * FROM subjects ORDER BY position ASC";
		$subject_set = mysql_query($query, $connection);
		confirm_query($subject_set);
		return $subject_set;
	}
	
	function get_pages_for_subject($subject_id)
	{
		global $connection;
		$query = "SELECT * FROM pages WHERE subject_id = {$subject_id} ORDER BY postion ASC";
		$page_set = mysql_query($query, $connection);
		confirm_query($page_set);
		return $page_set;
	}
	
	function get_subject_by_id($subject_id)
	{
		global $connection;
		$query = "SELECT * ";
		$query .= "FROM subjects ";
		$query .= "WHERE id=" . $subject_id . " ";
		$query .= "LIMIT 1";
		
		$result_set = mysql_query($query, $connection);
		confirm_query($result_set);
		if( $subject = mysql_fetch_array($result_set) )
		{
			return  $subject;
		}
		else
		{
			return NULL;
		}
	}
	
	function get_page_by_id($page_id)
	{
		global $connection;
		$query = "SELECT * ";
		$query .= "FROM pages ";
		$query .= "WHERE id=" . $page_id . " ";
		$query .= "LIMIT 1";
		
		$result_set = mysql_query($query, $connection);
		confirm_query($result_set);
		if( $page = mysql_fetch_array($result_set) )
		{
			return  $page;
		}
		else
		{
			return NULL;
		}
	}
	
	function createDatabase($name)
	{
		global $connection;
		$query = "CREATE DATABASE ";		
		$query .= $name;
		$result_set = mysql_query($query, $connection);
		confirm_query($db_select);
	}
	
	function selectDatabase($name)
	{
		// 2. Select a database to use 
		$db_select = mysql_select_db($name,$connection);
		confirm_query($db_select);
	}
	
	function find_selected_page()
	{
		global $selSubj;
		global $selPage;
		$selSubj = NULL;
		$selPage = NULL;
	
		if( isset($_GET['subj']) )
		{
			$selSubj = get_subject_by_id($_GET['subj']);
		} 
		elseif( isset($_GET['page']) )
		{
			$selPage = get_page_by_id($_GET['page']);
		}
	}	
	
	function navigation($selSubj, $selPage)
	{
		$output = "<ul class=\"subjects\">";
		
		$subject_set = get_all_subjects();
		while ($subject = mysql_fetch_array($subject_set))
		{
			$output .= "<li";
			if ($subject["id"] == $selSubj['id'])
			{
				$output .= " class=\"selected\"";
			}
			$output .= "><a href=\"index.php?subj=" . urlencode($subject["id"]) . 
				"\">{$subject["menu_name"]}</a></li>";
			
			$page_set = get_pages_for_subject($subject["id"]);
			$page_count = mysql_num_rows($page_set);
			if( $page_count <= 0 )
				continue;
			$output .= "<ul class=\"pages\">";
			while ($page = mysql_fetch_array($page_set))
			{
				$output .= "<li";
				if ($page["id"] == $selPage['id'])
				{
					$output .= " class=\"selected\"";
				}
				else 
				{
					$output .= " class=\"nonselected\"";
				}
				$output .= "><a href=\"index.php?page=" . urlencode($page["id"]) . 
				"\">{$page["menu_name"]}</a></li>";
			}
			$output .= "</ul>";
		}		
		$output .= "</ul>";
		return $output;
	}

?>